<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/10
 * Time: 14:21
 */

namespace Controller;

use Shop\Controller;

class LoginController extends Controller{

    //显示验证码
    public function idYzm(){
        $this->yzm(4);
    }

    //注册方法
    public function register(){
        if($_POST){
            if($_POST['email']!=$_SESSION['Home']['Log']['email']||$_POST['emailyzm']!=$_SESSION['Home']['Log']['sjs']){
                exit('邮箱或者验证码不正确!');
            }
            if($_POST['pass1']!=$_POST['pass2']){
                exit('密码不一致!');
            }
            $arr=array();
            $arr['email']=$_POST['email'];
            $arr['pass']=$_POST['pass1'];
            $arr['time']=date("Y-m-d H:i:s",time());
            $arr['ip']=$_SERVER['REMOTE_ADDR'];
            if($this->pdo->pdo_ins($arr,'s_member')){
                exit('ok');
            }else{
                exit('注册失败,请重试');
            }
        }
        $this->view->display();
    }

    //验证码和发送邮件方法

    public function yzR(){
        $email=$_POST['email'];
        $yzm=$_POST['yzm'];

        if($yzm!=$_SESSION['Home']['yzm']){
            $this->retuAjax(array('type'=>2,'msg'=>'验证码不正确'));
        }

        //判断邮箱是否合法---正则匹配(字符串的匹配),需要用到百度即可
        $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i";
        if(!preg_match($pattern,$email)){
            $this->retuAjax(array('type'=>2,'msg'=>'邮箱格式错误!'));
        }

        //判断邮箱是否已经注册
        $con=$this->pdo->pdo_sel("select count(*) as co from s_member where email = '$email'");
        if($con['co']>0){
            $this->retuAjax(array('type'=>2,'msg'=>'邮箱已经被注册!'));
        }

        //保存到session---除了需要保存随机数还需要保存邮箱
        $sjs=rand();
        $_SESSION['Home']['Log']=array('sjs'=>$sjs,'email'=>$email);

        //发送邮件
       $res=$this->sendMail($email,'注册验证码','你的注册验证码为<b>'.$sjs.'</b>');
        if($res!='ok'){
            $this->retuAjax(array('type'=>2,'msg'=>'邮件服务器异常'));
        }else{
            $this->retuAjax(array('type'=>1,'msg'=>'发送成功'));
        }
    }

    public function login(){
        if($_POST){
            if($_POST['yzm']!=$_SESSION['Home']['yzm']){
                exit('验证码不正确');
            }
            $sel=$this->pdo->pdo_sel("select * from s_member where email = '{$_POST['email']}'");
            if(empty($sel)){
                exit('账号不存在');
            }
            if($sel['pass']!=$_POST['pass']){
                exit("密码不正确");
            }
            //实现登录
            $this->pdo->pdo_upd(array('ip'=>$_SERVER['REMOTE_ADDR'],'time'=>date("Y-m-d H:i:s",time())),'s_member', " id = '{$sel['id']}'");
            //保存到session
            $_SESSION['Home']['Member']=$sel;

            //判断是自动登录
            if($_POST['remember']==1){
                setcookie('home_member',$sel['email'],time()+3*24*3600,'/');
            }
            exit('ok');
        }
        $this->view->display();
    }

    //退出功能
    public function tc(){
        unset($_SESSION['Home']['Member']);

        setcookie('home_member','',time()-1,'/');
        $this->success('退出成功!','Index','index');
    }
}